package com.dinner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dinner.auto.JSONAuthentication;
import com.dinner.entity.DiCar;
import com.dinner.entity.DiMenu;
import com.dinner.entity.OrderMenu;
import com.dinner.mapper.DiMenuMapper;
import com.dinner.mapper.OrderMenuMapper;
import com.dinner.response.Result;
import com.dinner.service.OrderMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinner
 * @since 2022-02-13
 */
@RestController
@RequestMapping("/order-menu")
public class OrderMenuController extends JSONAuthentication {
    @Autowired
    OrderMenuMapper orderMenuMapper;
    @Autowired
    OrderMenuService orderMenuService;
    @Autowired
    DiMenuMapper diMenuMapper;

    @ApiOperation("添加菜品订单")
    @PostMapping("/add")
    public void addOrderMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("orderId") int orderId, @RequestParam("menuId") int menuId,@RequestParam("menuNum")int menuNum) throws Exception{
        OrderMenu orderMenu=new OrderMenu();
        orderMenu.setOrderId(orderId);
        orderMenu.setMenuId(menuId);
        orderMenu.setMenuNum(menuNum);
        DiMenu diMenu=diMenuMapper.selectById(menuId);
        int xiaoliang=diMenu.getMenuSale();
        int xianyou=xiaoliang+menuNum;
        diMenu.setMenuSale(xianyou);
        diMenuMapper.updateById(diMenu);
        String typp=diMenu.getMenuType();
        if(orderMenuMapper.insert(orderMenu)>0){
            Result ok = Result.ok().data("type",typp);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看订单所购买的菜品通过订单id")
    @PostMapping("/lookMenuMenuByid")
    public void lookMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("orderId") int orderId) throws Exception{
        List<OrderMenu> list=orderMenuService.findAll(orderId);
        if(list.size()>0){
            Result ok = Result.ok().data("menu",list);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("订单无菜品");
            this.WriteJSON(request, response, error);
        }
    }
}

